package com.manor.main.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzy.okgo.model.Response;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.manor.common.CommonAppConfig;
import com.manor.common.activity.AbsActivity;
import com.manor.common.http.HttpCallback;
import com.manor.common.http.JsonBean;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.main.R;
import com.manor.main.adapter.WithdrawalAccountAdapter;
import com.manor.main.bean.WithdrawalAccountBean;
import com.manor.main.http.MainHttpUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 提现账户
 */
@Route(path = RouteUtil.PC_WithdrawalAccount)
public class WithdrawalAccountActivity extends AbsActivity {
    RecyclerView mRecyclerView;
    WithdrawalAccountAdapter mAdapter;
    private SmartRefreshLayout srlFollow;
    int page = 0;
    TextView tv_add;
    private List<WithdrawalAccountBean> withdrawalAccountBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawallist;
    }

    @Override
    protected void main() {
        setTitle("提现账户");
        srlFollow = findViewById(R.id.srl_follow);
        mRecyclerView = findViewById(R.id.rc_listview);
        tv_add = findViewById(R.id.tv_add);
        String id = getIntent().getStringExtra("id");
        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtil.forwardAddAccount(WithdrawalAccountActivity.this, RouteUtil.PC_ADDACCOUNT, 10);
            }
        });

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

        srlFollow.setEnableLoadMore(false);
        srlFollow.setEnableRefresh(false);

        srlFollow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                setData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                setData(true);
            }
        });
        mAdapter = new WithdrawalAccountAdapter(R.layout.rv_account_item, null, id, new WithdrawalAccountAdapter.IAddListListener() {
            @Override
            public void deleteBank() {
                setData(true);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                // 获取用户计算后的结果
                String id = mAdapter.getItem(position).getId();
                intent.putExtra("id", id); //将计算的值回传回去
                String name = mAdapter.getItem(position).getAccount();
                intent.putExtra("name", name);
                String context = mAdapter.getItem(position).getName();
                intent.putExtra("context", context);
                setResult(2, intent);
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        setData(true);
    }

    private void setData(final boolean istype) {
        if (istype) {
            page = 1;
        } else {
            page++;
        }
        MainHttpUtil.getWithdrawalAccount(CommonAppConfig.getInstance().getUid(), CommonAppConfig.getInstance().getToken(), new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0 && info != null && StringUtil.isNotEmpty(info)) {

                    withdrawalAccountBeans = JSON.parseArray(info, WithdrawalAccountBean.class);
                    //   WithdrawalAccountBean withdrawalAccountBean = mGson.fromJson(info[0], WithdrawalAccountBean.class);
                    if (istype) {
                        mAdapter.setNewInstance(withdrawalAccountBeans);

                        srlFollow.finishRefresh();
                        //  withdrawalAccountBeans.clear();
                    } else {
                        mAdapter.addData(withdrawalAccountBeans);
                        srlFollow.finishLoadMore();
                    }


                } else {
                    if (istype && null != withdrawalAccountBeans && withdrawalAccountBeans.size() > 0) {
                        withdrawalAccountBeans.clear();
                        mAdapter.notifyDataSetChanged();
                    }
                    ToastUtil.show(msg);

                }
            }

            @Override
            public void onError(Response<JsonBean> response) {
                super.onError(response);
                srlFollow.finishLoadMore();
                srlFollow.finishRefresh();
            }
        });

    }
}
